<?php

function greengar_get_friends($userid, $app, $pending = FALSE) {
    $relationship = greengar_get_relationship($app, $pending);
    $options = array(
        'relationship' => $relationship,
        'relationship_guid' => $userid,
        'inverse_relationship' => FALSE,
        'types' => 'user',
        'subtypes' => $subtype,
        'limit' => 10000
    );
    $entities = elgg_get_entities_from_relationship($options);
    return $entities;
}

function greengar_get_friends_reverse($userid, $app, $pending = FALSE) {
    $relationship = greengar_get_relationship($app, $pending);
    $options = array(
        'relationship' => $relationship,
        'relationship_guid' => $userid,
        'inverse_relationship' => TRUE,
        'types' => 'user',
        'subtypes' => $subtype,
        'limit' => 10000
    );
    $entities = elgg_get_entities_from_relationship($options);
    return $entities;
}

function greengar_count_friends($userid, $app, $pending = FALSE) {
    $relationship = greengar_get_relationship($app, $pending);
    $options = array(
        'relationship' => $relationship,
        'relationship_guid' => $userid,
        'inverse_relationship' => FALSE,
        'types' => 'user',
        'subtypes' => $subtype,
        'limit' => 10000,
	'count' => TRUE
    );
    $count_entities = elgg_get_entities_from_relationship($options);
    return $count_entities;
}

function greengar_count_mutual_friends($userid, $friend_id, $app) {
    
    $get_my_friends = greengar_get_friends($userid, $app);
    $get_friends_of_friends = greengar_get_friends($friend_id, $app);
    
    $my_friend_ids = array();
    $friend_friend_ids = array();

    foreach($get_my_friends as $get_my_friend) {
        $my_friend_ids[] = $get_my_friend->getGUID();
    }
    
    foreach($get_friends_of_friends as $get_friends_of_friend) {
        $friend_friend_ids[] = $get_friends_of_friend->getGUID();
    }
    
    $count = 0;
    $my_friend_ids_count = count($my_friend_ids);
    $friend_friend_ids_count = count($friend_friend_ids);
    
    if($my_friend_ids_count < $friend_friend_ids_count) {
        for($i=0;$i<$my_friend_ids_count;$i++) {
            if(in_array($my_friend_ids[$i], $friend_friend_ids))
              $count++;
        }
    } else {
        for($i=0;$i<$friend_friend_ids_count;$i++) {
            if(in_array($friend_friend_ids[$i], $my_friend_ids))
              $count++;
        }
    }
        
    return $count;
    
}

?>
